'use client';

import { cn } from '@/lib/utils';
import { InputHTMLAttributes, forwardRef, useState } from 'react';
import { Eye, EyeOff } from 'lucide-react';

export interface PasswordInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'type'> {
  label?: string;
  error?: string;
}

/**
 * PasswordInput - 带可见性切换的密码输入框
 * 
 * Features:
 * - 密码显示/隐藏切换
 * - 玻璃拟态效果
 * - 错误提示
 */
const PasswordInput = forwardRef<HTMLInputElement, PasswordInputProps>(
  ({ className, label, error, ...props }, ref) => {
    const [showPassword, setShowPassword] = useState(false);

    return (
      <div className="w-full">
        {label && (
          <label className="block text-sm font-semibold bg-gradient-to-r from-indigo-600 to-purple-600 bg-clip-text text-transparent dark:from-indigo-400 dark:to-purple-400 mb-2">
            {label}
          </label>
        )}
        <div className="relative">
          <input
            type={showPassword ? 'text' : 'password'}
            ref={ref}
            className={cn(
              'w-full px-4 py-3 pr-12',
              'bg-white/60 dark:bg-white/10 backdrop-blur-md backdrop-saturate-150',
              'border border-gray-300/40 dark:border-white/20 rounded-lg',
              'text-gray-900 dark:text-white placeholder-gray-400 dark:placeholder-white/50',
              'transition-all duration-300',
              'focus:outline-none focus:ring-2 focus:ring-indigo-500/40 dark:focus:ring-white/30 focus:border-indigo-500/50 dark:focus:border-white/40',
              'hover:bg-white/70 dark:hover:bg-white/15',
              'shadow-sm',
              error && 'border-red-400/50 focus:ring-red-400/50',
              className
            )}
            {...props}
          />
          <button
            type="button"
            onClick={() => setShowPassword(!showPassword)}
            className="absolute right-3 top-1/2 -translate-y-1/2 p-1.5 rounded-lg hover:bg-gray-200/50 dark:hover:bg-white/10 transition-colors"
            title={showPassword ? '隐藏密码' : '显示密码'}
          >
            {showPassword ? (
              <EyeOff className="h-5 w-5 text-gray-500 dark:text-white/60" />
            ) : (
              <Eye className="h-5 w-5 text-gray-500 dark:text-white/60" />
            )}
          </button>
        </div>
        {error && (
          <p className="mt-1 text-sm text-red-500 dark:text-red-300">{error}</p>
        )}
      </div>
    );
  }
);

PasswordInput.displayName = 'PasswordInput';

export default PasswordInput;

